package com.yyfax.ph.home.recommendloan.creditinfo;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.ArrayList;
import java.util.List;

/**
 * M:有无征信上传接口
 * Created by ying_he on 2017/11/7.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class UploadCreditPaperModel extends Model<BaseResultEntity>{

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.uploadCreditPaper(token, (CreditPaper) params);
	}

	@Override
	public void onNext(BaseResultEntity entity) {
		UploadCreditPaperResult result = cast(entity);
		EventAgent.post(result);

	}

	private interface Service {
		@POST("apply/record/credit")
		Observable<UploadCreditPaperResult> uploadCreditPaper(@Header("token") String token, @Body CreditPaper creditPaper);
	}

	public static class CreditPaper implements Parcelable {
		public String rcmdId = "";
		public List<String> imageIds = new ArrayList<>();

		public CreditPaper() {
		}

		protected CreditPaper(Parcel in) {
			rcmdId = in.readString();
			imageIds = in.readArrayList(String.class.getClassLoader());
		}

		public void writeToParcel(Parcel parcel, int i) {
			parcel.writeString(rcmdId);
			parcel.writeList(imageIds);

		}

		public static final Creator<CreditPaper> CREATOR = new Creator<CreditPaper>() {
			@Override
			public CreditPaper createFromParcel(Parcel in) {
				return new CreditPaper(in);
			}

			@Override
			public CreditPaper[] newArray(int size) {
				return new CreditPaper[size];
			}
		};

		@Override
		public int describeContents() {
			return 0;
		}
	}

	public static class UploadCreditPaperResult extends BaseResultEntity{}

}
